#include "../include/main.h"

using namespace std;

FILE *fa1;

int main() {
    parse();//语法分析生成抽象语法树
    showAst();
    ast::genSym(node);//生成符号表
    showSym();
    ast::semanticCheck(node);//静态语义分析
    TypeChecker::checkMain();
    ast::genCode(node);//生成目标代码
    CodeGenerator::entry();
    showCode();
//    Interpreter::interpret();//解释程序
    return 0;
}

[[maybe_unused]] void yyerror(char *s) {
    cerr << s;
    exit(8);
}

void parse() {
    cout << "请输入文件名称:";
    char s[30];
    cin >> s;
    string s_ = "../test/" + string(s);
    char *path = const_cast<char *>(s_.c_str());
    fa1 = fopen(path, "r");
    yyrestart(fa1);
    yyparse();
    fclose(fa1);
}

void showAst() {
    cout << "是否打印AST？ Y/N:";
    string s;
    cin >> s;
    if (s == "Y" || s == "y") {
        ast::showAst(node, 0);
    }
}

void showSym() {
    cout << "是否打印符号表？ Y/N:";
    string s;
    cin >> s;
    if (s == "Y" || s == "y") {
        BuildSym::showSym();
    }
}

void showCode() {
    cout << "是否打印目标代码？ Y/N:";
    string s;
    cin >> s;
    if (s == "Y" || s == "y") {
        CodeGenerator::showCode();
    }
}